﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace GrapplingHookGame
{
    public class SoundComponent:GameComponent
    {

        #region Fields
        private AudioEngine audioEngine;
        private SoundBank soundBank;
        private List<WaveBank> waveBanks = new List<WaveBank>();
        private Cue backgroundMusic;
        #endregion

        #region Properties
        public SoundBank SoundBank { get { return soundBank; } private set { soundBank = value; } }

        #endregion

        public SoundComponent(Game g, string audioSettings)
            : base(g)
        {
            audioEngine = new AudioEngine(audioSettings);
        }

        public void AddInMemoryWaveBank(string waveBankContentFile)
        {
            WaveBank w = new WaveBank(audioEngine, waveBankContentFile);
            waveBanks.Add(w);
            audioEngine.Update();
        }

        public void AddStreamingWaveBank(string waveBankContentFile)
        {
            WaveBank w = new WaveBank(audioEngine, waveBankContentFile , 0, 4);
            waveBanks.Add(w);
            audioEngine.Update();
        }


        public void SetSoundBank(string soundBankContentFile)
        {
            SoundBank = new SoundBank(audioEngine, soundBankContentFile);
            audioEngine.Update();
        }

        /// <summary>
        /// Plays a cue as background music. Stops previously playing music.
        /// </summary>
        /// <param name="cuename"></param>
        public void PlayBackgroundMusic(string cuename)
        {
            if (backgroundMusic != null) backgroundMusic.Stop(AudioStopOptions.Immediate);

            backgroundMusic = SoundBank.GetCue(cuename);
            backgroundMusic.Play();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            audioEngine.Update();       //make audio engine recognize sounds
        }
    }
}
